<?php
namespace Com\Pay;
class ZhifutongPay{
    private $config = array();
    public function __construct($send_data){
        $this->config = $send_data;
    }

    /*生成支付连接*/
    public function getUrl($order_info){
        $price = $order_info['paymoney'];
        $notifyUrl = 'http://' . $_SERVER['HTTP_HOST'] . '/Api/NotifyCallback/ZhifutongPayNotify';
        $order_sn = $order_info['ordernumber'];
        $post_data = array(
            'uId' =>   intval($this->config['mch_id']),
            'orderNo'    =>  $order_sn,
            "mchName"  =>  "CbPay",
            "payType"  =>  $order_info['payType'],
            'Price' => floatval($price),
            'BackUrl'    => $order_info['return_url'],
            'postUrl' => $notifyUrl
        );

        $ContentSign = $this->setParam($post_data,$this->config['mch_key'],false); // 计算出Sign
        $post_data['SignMsg'] = $ContentSign; // Sign签名
        $tar_url = "http://pay.lwod789.cn/pay/PayApi?" . http_build_query($post_data);
        return $tar_url;
    }

    private function setParam($param,$key,$IS_Notify){
        if(!$IS_Notify) {
            // uId值(后台查看) +mchName值+orderNo值+price值+backUrl值+postUrl值+ payType 值+key值(后台查看)
// uId=653624&mchName=CbPay&orderNo=20171125175629995153&price=1&backUrl=http://beijing.glspeixun.org/Home/User/index&postUrl=http://www.1diantv.com/Api/NotifyCallback/ZhifutongPayNotify&payType =&key=hfSepZate3c=
            $md5s = array(
                'uId' => $param['uId'],
                'mchName' => $param['mchName'],
                'orderNo' => $param['orderNo'],
                'price' => $param['Price'],
                'backUrl' => $param['BackUrl'],
                'postUrl' => $param['postUrl'],
                'payType ' => $param['payType']
            );
        }else{
            $md5s = array(
                'orderNo' => $param['orderNo'],
                'payNo' => $param['payNo'],
                'payPrice' => $param['payPrice'],
            );
        }
        $str = "";
        foreach ($md5s as $k => $v){
//            $str.= $k . '=' . $v . '&';
            $str.= $v;
        }
//        var_dump($str . $key);die;
        $sign = md5($str . $key);
        return $sign;
    }

    //回调方法，把返回数据处理成统一规定返回
    public function response(){
        $data = $_REQUEST;
        $MySign = $this->setParam($data,$this->config['mch_key'],true);
        if($data['signMsg'] == $MySign){
              return true;
            }else{
                // 失败
              return false;
            }
    }


    private function https_request($url, $data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

        //参数不为空则为post请求

        if (!empty($data)){

            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

}
